package com.tjetc.reflect;

import com.tjetc.reflect._1Cat;

import java.lang.reflect.Constructor;

public class _12ConstructorMethod {
    public static void main(String[] args) throws Exception{
        /*
        1.getModifiers:以int形式返回修饰符
        2.getName:返回构造器名(全类名)
        3.getParameterTypes:以Class[]返回参数类型数组
         */
        Class cls = Class.forName("com.tjetc.reflect._1Cat");
        Object o = cls.newInstance();

        Constructor[] declaredConstructors = cls.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println("-------------构造器相关------------");
            System.out.println("构造器名称：" + declaredConstructor.getName()
                    + "，构造方法的修饰符：" + declaredConstructor.getModifiers());
            System.out.println("-------------参数相关------------");
            Class[] parameterTypes = declaredConstructor.getParameterTypes();
            for (Class parameterType : parameterTypes) {
                System.out.println(parameterType);
            }
        }

//        Object o1 = cls.getDeclaredConstructor(String.class).newInstance("lisi");
        // 这种构造对象是推荐写法
        Object o1 = cls.getConstructor(String.class, Integer.class).newInstance("lisi", 1);
        _1Cat cat = (_1Cat) o1;
        System.out.println(cat);
        System.out.println(cat.name);
    }

}
